package com.lau.stream;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by liutf on 2016/7/6.
 */
public class StreamTest {

    public static void test(){
        //Lists是Guava中的一个工具类
        List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);
        long count = nums.stream().filter(num -> num != null).count();
        System.out.println(count);

        nums.stream().filter(num -> null!= num && num > 1)
            .collect(Collectors.toList()).forEach(System.out::println);

        Stream.iterate(1,item -> item + 1).limit(10).forEach(System.out::println);

        Stream<Integer> integerStream = Stream.of(1, 2, 3, 5);
        Stream<String> stringStream = Stream.of("taobao");
        stringStream.forEach(System.out::println);
        integerStream.forEach(System.out::println);

    }

    public static void testStreamConvert(){
        List<Integer> nums = Lists.newArrayList(1,1,null,2,3,4,null,5,6,7,8,9,10);

        int sum = nums.stream().filter(num -> num != null)
                .distinct().mapToInt(num -> num * 2)
                .peek(System.out::println).skip(2).limit(4).sum();
        System.out.println("sum:" + sum);

//        intStream.forEach(System.out::println);



//        System.out.println("sum is:"+nums.stream().filter(num -> num != null).
//                distinct().mapToInt(num -> num * 2).
//                peek(System.out::println).skip(2).limit(4).sum());
    }



    public static void main(String[] args) {
//        StreamTest.test();
        StreamTest.testStreamConvert();
    }


}
